import axios from "axios";
import {ElMessage} from "element-plus";

const defaultError = () => {
    ElMessage.error('发生了错误,请联系管理员')
}
const defaultFail = (message: string) => {
    ElMessage.warning(message)
}


function post(url: string, data: {}, success: Function, fail: Function = defaultFail, error: Function = defaultError) {
    axios.post(url, data, {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
            'Authorization': 'Bearer ' + localStorage.getItem('token'),
        },
        withCredentials: true,
    }).then(({data}) => {
        console.log(data)
        if (data.success) {
            success(data.massage);
        } else {
            fail(data.massage, data.status);
        }
    }).catch(() => error)
}

async function put(url: string, data: any, success: Function, fail: Function = defaultError) {
    try {
        const res = await axios.put(url, data, {withCredentials: true});
        if (res.status !== 200) {
            fail("服务器错误");
        } else {
            success("更新成功");
        }
    } catch (err) {
        console.log(err);
    }
}

function get(url: string, data: string, success: Function, fail: Function = defaultFail, error: Function = defaultError) {
    axios.get(url, {
        withCredentials: true,
    }).then(({data}) => {
        if (data.success) {
            success(data.massage, data.status);
        } else {
            fail(data.massage, data.status);
        }
    }).catch(error())
}


function deleteData(url: string, data: {}, success: Function, fail: Function = defaultFail, error: Function = defaultError) {
    axios.delete(url, {params: data}).then(({data}) => {
        if (data.success) {
            success(data.message, data.status);
        } else {
            fail(data.message, data.status);
        }
    }).catch((err) => {
        error(err);
    })
}


async function getAllUsers(url: string) {
    try {
        const res = await axios.get(url, {withCredentials: true});
        if (res.data.status !== 200) {
            defaultFail("服务器错误");
        } else {
            if (res.data.success) {
                return res.data;
            } else {
                defaultFail("服务器错误");
            }
        }
    } catch (error) {
        console.log("error : " + error);
        defaultError();
    }
}

async function getAllHouse(url: string) {
    try {
        const res = await axios.get(url, {withCredentials: true});
        if (res.data.status !== 200) {
            defaultFail("服务器错误");
        } else {
            if (res.data.success) {
                return res.data;
            } else {
                defaultFail("服务器错误");
            }
        }
    } catch (error) {
        console.log("error : " + error);
        defaultError();
    }
}

async function getAllEvent(url: string) {
    try {
        const res = await axios.get(url, {withCredentials: true});
        if (res.data.status !== 200) {
            defaultFail("服务器错误");
        } else {
            if (res.data.success) {
                return res.data
            }
        }
    } catch (e) {
        console.log(e);
    }
}

async function getAllAnnouncement(url: string) {
    try {
        const res = await axios.get(url, {withCredentials: true});
        if (res.data.status !== 200) {
            defaultFail("服务器错误");
        } else {
            if (res.data.success) {
                return res.data
            }
        }
    } catch (e) {
        console.log(e)
    }
}

async function getAllRepair(url: string) {
    try {
        const res = await axios.get(url, {withCredentials: true});
        if (res.data.status !== 200){
            defaultFail("服务器错误");
        }else {
            if (res.data.success){
                return res.data
            }
        }
    }catch (e){
        console.log(e)
    }
}

export {
    get, post, put, getAllEvent, getAllHouse,
    getAllUsers, deleteData, getAllAnnouncement,
    getAllRepair
}